#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/12/4 14:58
# @Author  : leon yan
# @Site    : 
# @File    : qPagenation.py
# @Software: PyCharm

# 面向对象:
class QPagenation(object):
    def __init__(self, data_list, page, per_data_num=10):
        '''
        初始化
        :param data_list: 所有数据列表
        :param page: 当前要查看的列表页
        :param per_data_num: 每页默认要显示几条
        '''
        self.data_list = data_list
        self.page = page
        self.per_data_num = per_data_num

    @property
    def start(self):
        '''
        计算引索的起始位置
        :return:
        '''
        return (self.page - 1) * self.per_data_num

    @property
    def end(self):
        '''
        计算引索的结束位置
        :return:
        '''
        return self.page * self.per_data_num

    @property
    def totalpage(self):
        aa = len(self.data_list) % self.per_data_num
        aa2 = len(self.data_list) // self.per_data_num
        if aa > 0:
            return aa2 + 1
        else:
            return aa2

    def show(self):
        '''
        切片取数据,展示对应分页的结果
        :return:
        '''
        istart = self.start
        iend = self.end
        iTotalPage = self.totalpage
        result = self.data_list[istart:iend]
        lst = []
        for row in result:
            lst.append(row)
            # print(row)
        return lst, iTotalPage

    def paginate(self, page, size):
        if not isinstance(page, int):
            try:
                page = int(page)
            except TypeError:
                page = 1

        if not isinstance(size, int):
            try:
                size = int(size)
            except TypeError:
                size = 20

        if page > 0:
            page -= 1

        data = {
            "limit": size,
            "offset": page * size,
            "before": page,
            "current": page + 1,
            "next": page + 2
        }

        return data


if __name__ == '__main__':
    data_list = []
    for i in range(1, 901):
        data_list.append('豆谷云-%s' % i)
    while True:
        # 1.输入要查看的页码
        page = int(input("请输入要查看的页码:"))

        obj = QPagenation(data_list, page)

        obj.show()
